import { Inject } from '../decorates/inject';
import { Draw } from './draw';
import { Config } from './config';
import { numToCharator } from '../utils/transate';
import { Hooks } from './hooks';
import { Select } from './select';
import { debounceTime } from 'rxjs/operators';
import { Events } from './events';
@Inject()
export class Header {
    constructor(protected draw: Draw, protected conf: Config, protected hooks:Hooks, private select: Select, private events: Events) {

    }
    public drawHeader() {
        this.drawCorner();
        this.renderColHeader();
        this.renderRowHeader();
    }
    public zInit() {
        this.select.selectedChange.pipe(debounceTime(5)).subscribe(res => {
            this.drawHeader();
        });
        // this.events.
    }
    protected drawCorner() {
        this.draw.rect(1,1, this.conf.colHeaderWidth + 1, this.conf.rowHeaderHeight + 1, null, {
            bgClr: this.conf.colHeaderColor,
            border: {
                top: {
                    color: this.conf.headerBorderColor,
                    width: 1,
                    type: 'thin'
                },
                left: {
                    color: this.conf.headerBorderColor,
                    width: 1,
                    type: 'thin'
                }
            }
        });
    }
    protected renderRowHeader() {
        let drawSize = this.draw.getCasSize();
        let row = this.conf.startRow;
        let style: any = {
            bgClr: this.conf.rowHeaderColor,
            border: {
                top: {
                    color: this.conf.headerBorderColor,
                    width: 1,
                    type: 'thin'
                },
                left: {
                    color: this.conf.headerBorderColor,
                    width: 1,
                    type: 'thin'
                }
            }
        }
        for(let i = this.conf.rowHeaderHeight + 1; i < drawSize.height;) {
            if(this.select.selectRanges.length) {
                let range =  this.select.selectRanges[0];
                let minRow = Math.min(range.start.row, range.end.row);
                let maxRow = Math.max(range.start.row, range.end.row);
                if(row >=minRow && row <= maxRow) {
                    style.bgClr = '#dcdcdc';
                }else{
                    style.bgClr = this.conf.rowHeaderColor;
                }
            }else{
                style.bgClr = this.conf.rowHeaderColor;
            }
            let height =  this.conf.getRowHeight(row);
            this.draw.rect(1, i + 1, this.conf.colHeaderWidth + 1, height + 1, {
                color: "#000",
                vertical: 'middle',
                horizontal: 'center',
                size: 12,
                family: 'Microsoft YaHei',
                content: String(row + 1)
            }, style);
            i = i + height + 1;
            row++;
            this.conf.visualMaxRow = Math.max(row, this.conf.visualMaxRow);
        }
    }
    protected renderColHeader() {
        let drawSize = this.draw.getCasSize();
        let col = this.conf.startCol;
        let style: any = {
            bgClr: this.conf.rowHeaderColor,
            border: {
                top: {
                    color: this.conf.headerBorderColor,
                    width: 1,
                    type: 'thin'
                },
                left: {
                    color: this.conf.headerBorderColor,
                    width: 1,
                    type: 'thin'
                }
            }
        }
        for(let i = this.conf.colHeaderWidth + 1; i < drawSize.width;) {
            let width =  this.conf.getColWidth(col);
            if(this.select.selectRanges.length) {
                let range =  this.select.selectRanges[0];
                let minRow = Math.min(range.start.col, range.end.col);
                let maxRow = Math.max(range.start.col, range.end.col);
                if(col >=minRow && col <= maxRow) {
                    style.bgClr = '#dcdcdc';
                }else{
                    style.bgClr = this.conf.rowHeaderColor;
                }
            }else{
                style.bgClr = this.conf.rowHeaderColor;
            }
            this.draw.rect(i+1, 1, width + 1, this.conf.rowHeaderHeight + 1, {
                color: "#000",
                vertical: 'middle',
                horizontal: 'center',
                size: 12,
                family: 'Microsoft YaHei',
                content: numToCharator(col)
            }, style);
            i = i + width + 1;
            col++;
            this.conf.visualMaxCol = Math.max(col, this.conf.visualMaxCol);
        }
    }
}